<?php
/**
 * pdo类
 *
 *
 */
class LibsPdo {
    private static $_instance = array();
    private $_pdo = null;
    private $_log = '';
    /**
     * 初始化
     *
     * @param string $dsn dsn
     * @param string $user user 
     * @param string $pass pass
     * @param array  $options options 
     *
     * @return void
     */
    private function __construct($dsn, $user, $pass, $options=array()) {
        $this->_log = ROOT_PATH.DIRECTORY_SEPARATOR.'logs'.DIRECTORY_SEPARATOR.'error_sql.log';   

        try {
            $this->_pdo = new PDO($dsn, $user, $pass, $options);
        } catch (PDOException $e) {
            if (DEBUG) {
                throw $e;
            } else {
                $message = date('Y-m-d H:i:s')."\t";
                $message .= $e->getMessage().PHP_EOL;
                error_log($message, 3, $this->_log);
            }
        }
    }

    /**
     * 获取PDO实例
     *
     * @return PDO()
     */
    public function getPdo() {
        return $this->_pdo;
    }

    /**
     * single模式
     *
     * @param string $dsn dsn
     * @param string $user user 
     * @param string $pass pass
     * @param array  $options options 
     *
     * @return this
     */
    public static function getInstance($dsn='', $user='', $pass='', $options=array()) {
        $dsnHash = md5($dsn);
        if(empty(self::$_instance[$dsnHash])){
            $cls = __CLASS__;
            self::$_instance[$dsnHash] = new $cls($dsn, $user, $pass, $options);
        }
        return self::$_instance[$dsnHash];
    }
} 
